package ipad.asia.controller;
import ipad.asia.entity.*;
import ipad.asia.service.bbs.BbsFeign;
import ipad.asia.service.consumer.ConsumerFeign;
import ipad.asia.service.discount.DiscountFeign;
import ipad.asia.service.enterprise.EnterpriseFeign;
import ipad.asia.service.evaluate.EvaluateFeign;
import ipad.asia.service.product.ProductFeign;
import ipad.asia.service.shop.ShopFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Controller
public class DetailController {
    @Autowired
    private ProductFeign productFeign;
    @Autowired
    private BbsFeign bbsFeign;
    @Autowired
    private DiscountFeign discountFeign;
    @Autowired
    private ConsumerFeign consumerFeign;
    @Autowired
    private EnterpriseFeign enterpriseFeign;
    @Autowired
    private ShopFeign shopFeign;
    @Autowired
    private EvaluateFeign evaluateFeign;
    @RequestMapping("/detail")
    public String detail(@RequestParam(value = "id",required = false)String idd,
                         @RequestParam(value = "message",required = false)String message,
                         HttpServletRequest request, Model model) {
        System.out.println("idd::::"+idd);
        HttpSession session = request.getSession();
        String detail_id= (String) session.getAttribute("detail_productId");
        System.out.println("session:"+detail_id);
        if (idd == null&&detail_id==null) {
            return "redirect:/index";
        } else {
            Integer id;
            if(idd==null){id=Integer.parseInt(detail_id);}else{id=Integer.parseInt(idd);}
            Product product = productFeign.getById(id);
            Shop shop = shopFeign.findById(product.getShopId());
            model.addAttribute("shop",shop);

            Consumer consumer = consumerFeign.getName((String) session.getAttribute("user"));

            List<Product> products = productFeign.all();
            Random random = new Random();
            List<Integer> rans=new ArrayList<>();
            for(int i=0;i<products.size();i++){
                rans.add(random.nextInt(products.size()));
            }
//            List<Bbs> byProductId = bbsFeign.findByProductId(id);
            Discount discount = discountFeign.findDiscount(id);
            model.addAttribute("consumer", consumer);
            model.addAttribute("discount", discount);
//            model.addAttribute("commonNumber", byProductId.size());
            if( products.size()>8) {
                model.addAttribute("random1", products.get(1));
                model.addAttribute("random2", products.get(2));
                model.addAttribute("random3", products.get(3));
                model.addAttribute("random4", products.get(4));
                model.addAttribute("random5", products.get(5));
                model.addAttribute("random6", products.get(6));
                model.addAttribute("random7", products.get(7));
                model.addAttribute("random8", products.get(8));
            }

            model.addAttribute("products", products);
            String[] color = product.getColor().split("，");
            for(int i=0;i<color.length;i++){
                System.out.println(color[i]);
            }

            model.addAttribute("color",color);
            model.addAttribute("product", product);
            session.setAttribute("message",null);
            System.out.println("message"+message);
            model.addAttribute("message",message);
            model.addAttribute("user", session.getAttribute("user"));
            model.addAttribute("warning", session.getAttribute("warning"));

            List<Evaluate> evaluates = evaluateFeign.findByProductId(id);
            //获取评分
            int avg;
            List<Integer> level_avg=new ArrayList<>();
            for (int i=0;i<evaluates.size();i++){
               level_avg.add(evaluates.get(i).getLevel());
            }
            int sum=0;
            for(int i=0;i<level_avg.size();i++){
                sum+= level_avg.get(i);
            }
            if(level_avg.size()==0){
                avg=0;
            }else {
             avg=sum/level_avg.size();}
            //好中差评
            List<Integer> good=new ArrayList<>();
            List<Integer> centre=new ArrayList<>();
            List<Integer> bad=new ArrayList<>();
            for (int i=0;i<evaluates.size();i++){
                if(evaluates.get(i).getLevel()>=4){
                    good.add(evaluates.get(i).getLevel());
                }if(evaluates.get(i).getLevel()>=2&&evaluates.get(i).getLevel()<4){
                    centre.add(evaluates.get(i).getLevel());
                }else {
                    bad.add(evaluates.get(i).getLevel());
                }
            }
            model.addAttribute("good",good);
            model.addAttribute("centre",centre);
            model.addAttribute("bad",bad);
            model.addAttribute("avg",avg);
            model.addAttribute("all",evaluates.size());
            model.addAttribute("goodSize",good.size());
            model.addAttribute("centreSize",centre.size());
            model.addAttribute("badSize",bad.size());
            model.addAttribute("evaluates",evaluates);
            return "detail";
        }
    }
}
